﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Medianamik.Core.Configuration;

namespace Medianamik.Core.Urls
{
    public class UrlDictionaryProcess : IUrlDictionaryProcess
    {
        private Uri _url;
        
        #region IUrlDictionnaryProcess Members

        public bool ThisUrlRedirectMustBeProcessWithDictionnary(Uri url)
        {
            _url = url;
            return ThisUrlMustBeProcessWithDictionnary(true);
        }

        public bool ThisUrlMustBeProcessWithDictionnary(Uri url)
        {
            _url = url;
            return ThisUrlMustBeProcessWithDictionnary(false);
        }

        public bool ThisUrlIs404PageWhitoutTemplate(Uri url)
        {
            bool errorPageWhitoutTemplate = false;

            if (url.Query != null && url.Query.IndexOf("?404;") > -1)
            {
                Uri errorUrl = null;
                Uri.TryCreate(url.Query.Substring(url.Query.IndexOf("?404;") + 5), UriKind.Absolute, out errorUrl);
                if (errorUrl != null)
                {
                    if (errorUrl.Segments.Count() > 0)
                    {
                        var filterUrl = (MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);
                        var lastSegment = errorUrl.Segments.Last();

                        if (filterUrl.FilterUrlConfiguration.AllowNoExtension)
                            errorPageWhitoutTemplate = IsNoExtension(lastSegment);

                        if (!errorPageWhitoutTemplate)
                        {
                            var extensions = filterUrl.FilterUrlConfiguration.FilterUrlsForError.Cast<FilterUrl>().
                                Select(p => p.Extension).ToList();

                            errorPageWhitoutTemplate = IsValidExtensions(lastSegment, extensions);
                        }
                    }

                    errorPageWhitoutTemplate = !errorPageWhitoutTemplate;
                }   
            }

            return errorPageWhitoutTemplate;
        }

        #endregion


        private bool ThisUrlMustBeProcessWithDictionnary(bool forRedirect)
        {
            var allowProcess = false;
            var filterUrl = (MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK);

            if (_url.Segments.Count() > 0)
            {
                var lastSegment = _url.Segments.Last();

                if (filterUrl.FilterUrlConfiguration.AllowNoExtension)
                    allowProcess = IsNoExtension(lastSegment);

                if (!allowProcess)
                {
                    var extensions = forRedirect
                                         ? filterUrl.FilterUrlConfiguration.FilterUrlsForRedirect.Cast<FilterUrl>().
                                               Select(p => p.Extension).ToList()
                                         : filterUrl.FilterUrlConfiguration.FilterUrls.Cast<FilterUrl>().
                                               Select(p => p.Extension).ToList();
                    allowProcess = IsValidExtensions(lastSegment, extensions);
                }
            }

            return allowProcess;
        }

        private bool IsNoExtension(string lastSegment)
        {
            return !lastSegment.Contains(".");
        }

        private bool IsValidExtensions(string lastSegment, List<string> allowExtensions)
        {
            // Default Extension
            if (allowExtensions.Count == 0)
                allowExtensions.Add("html");

            foreach (string ext in allowExtensions)
            {
                if (lastSegment.EndsWith(string.Concat(".", ext)))
                    return true;
            }

            return false;
        }

    }
}
